package core.models;

import java.util.ArrayList;

import javax.swing.DefaultListModel;

import core.utilities.InetAddressValidator;

public class NetInterface {
	// Required fields
	private boolean auto;
	private String name = null;
	private String type = null;
	
	// Required if type == static
	private String address = null;
	private String netmask = null;
	
	// Optional for static
	private String gateway = null;
	private String broadcast = null;
	private String network = null;
	
	// Optional for all
	private ArrayList<String> upList;
	private ArrayList<String> downList;
	private ArrayList<String> preUpList;
	private ArrayList<String> preDownList;
	
	public NetInterface() {
		upList = new ArrayList<String>();
		downList = new ArrayList<String>();
		preUpList = new ArrayList<String>();
		preDownList = new ArrayList<String>();
	}
	
	@Override
	public String toString() {
		return name;
	}
	
	public boolean isValid() {
		InetAddressValidator validator = InetAddressValidator.getInstance();
		if (name.equals(""))
			return false;
		if (!(type.equals("static") || type.equals("dhcp") || type.equals("loopback")))
			return false;
		if (type.equals("static")) {
			if (address == null || !validator.isValidInet4Address(address))
				return false;
			if (netmask == null || !validator.isValidInet4Address(netmask))
				return false;
		}
		if (network != null)
			if (!validator.isValidInet4Address(network))
				return false;
		if (broadcast != null)
			if (!validator.isValidInet4Address(broadcast))
				return false;
		if (gateway != null)
			if (!validator.isValidInet4Address(gateway))
				return false;
		return true;
	}

	public boolean isAuto() {
		return auto;
	}

	public void setAuto(boolean auto) {
		this.auto = auto;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getNetmask() {
		return netmask;
	}

	public void setNetmask(String netmask) {
		this.netmask = netmask;
	}

	public String getNetwork() {
		return network;
	}

	public void setNetwork(String network) {
		this.network = network;
	}

	public String getBroadcast() {
		return broadcast;
	}

	public void setBroadcast(String broadcast) {
		this.broadcast = broadcast;
	}

	public String getGateway() {
		return gateway;
	}

	public void setGateway(String gateway) {
		this.gateway = gateway;
	}

	public ArrayList<String> getUpList() {
		return upList;
	}

	public ArrayList<String> getDownList() {
		return downList;
	}

	public ArrayList<String> getPreUpList() {
		return preUpList;
	}

	public ArrayList<String> getPreDownList() {
		return preDownList;
	}
	
	public String[] getPossibleTypes() {
		String[] types = {"dhcp", "static", "loopback"};
		return types;
	}
	
	public static String[] getListsDirectives() {
		String[] directives = {"up", "down", "pre-up", "pre-down"};
		return directives;
	}
	
	public DefaultListModel getUpsModel() {
		DefaultListModel model  = new DefaultListModel();
		for (String s : upList)
			model.addElement(s);
		return model;
	}
	
	public DefaultListModel getDownsModel() {
		DefaultListModel model = new DefaultListModel();
		for (String s : downList)
			model.addElement(s);
		return model;
	}
	
	public DefaultListModel getPreUpsModel() {
		DefaultListModel model = new DefaultListModel();
		for (String s : preUpList)
			model.addElement(s);
		return model;
	}
	
	public DefaultListModel getPreDownsModel() {
		DefaultListModel model = new DefaultListModel();
		for (String s : preDownList)
			model.addElement(s);
		return model;
	}

	public void setUpList(ArrayList<String> upList) {
		this.upList = upList;
	}
	
	public void setUpListUsingModel(DefaultListModel model) {
		this.upList.clear();
		Object[] arr = model.toArray();
		for (Object o : arr) {
			upList.add((String) o);
		}
	}

	public void setDownList(ArrayList<String> downList) {
		this.downList = downList;
	}
	
	public void setDownListUsingModel(DefaultListModel model) {
		this.downList.clear();
		Object[] arr = model.toArray();
		for (Object o : arr) {
			downList.add((String) o);
		}
	}

	public void setPreUpList(ArrayList<String> preUpList) {
		this.preUpList = preUpList;
	}
	
	public void setPreUpListUsingModel(DefaultListModel model) {
		this.preUpList.clear();
		Object[] arr = model.toArray();
		for (Object o : arr) {
			preUpList.add((String) o);
		}
	}

	public void setPreDownList(ArrayList<String> preDownList) {
		this.preDownList = preDownList;
	}
	
	public void setPreDownListUsingModel(DefaultListModel model) {
		this.preDownList.clear();
		Object[] arr = model.toArray();
		for (Object o : arr) {
			preDownList.add((String) o);
		}
	}
	
}
